package cn.percent.sqlSession;

import cn.percent.config.Configuration;
import cn.percent.config.MappedStatement;
import cn.percent.executor.Executor;
import cn.percent.executor.SimpleExecutor;
import com.sun.tools.javac.comp.Todo;

import java.beans.IntrospectionException;
import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.List;
import java.util.SimpleTimeZone;

public class DefaultSqlSession implements SqlSession{
    private Configuration configuration;
    private Executor executor;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
        this.executor = new SimpleExecutor();
    }

    public DefaultSqlSession(Configuration configuration,Executor executor) {
        this.configuration = configuration;
        this.executor = executor;
    }

    @Override
    public <E> List<E> selectList(String statmentId, Object... params) throws IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, SQLException, InvocationTargetException, ClassNotFoundException {
        //SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statmentId);
        List<Object> resultList = executor.query(configuration, mappedStatement, params);
        return (List<E>) resultList;
    }

    @Override
    public <T> T selectOne(String statmentId, Object... params) throws IllegalAccessException, ClassNotFoundException, IntrospectionException, InstantiationException, SQLException, InvocationTargetException, NoSuchFieldException {
        List<Object> object = selectList(statmentId, params);
        if (object.size() == 1){
            return (T) object.get(0);
        } else {
            throw new RuntimeException("查询结果为空活着返回结果过多");
        }
    }

    public void execute(String statmentId,Object... params) throws ClassNotFoundException, SQLException, IllegalAccessException, NoSuchFieldException {
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statmentId);
        executor.execute(configuration,mappedStatement,params);
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        // 使用JDK动态代理来为Dao接口生成代理对象并返回
        // mapperClass 代理接口的class
        // 该方法用于为指定类加载器、一组接口及调用处理器生成动态代理类实例

        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {

            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                try {
                    String methodName = method.getName();
                    //System.out.println(methodName);
                    // 接口全限定名
                    String className = method.getDeclaringClass().getName();
                    String statementId = className + "." + methodName;
                    Type genericReturnType = method.getGenericReturnType();
                    // 判断参数是否进行了泛型类型参数化 ParameterizedType 参数化类型
                    if (genericReturnType instanceof ParameterizedType) {
                        List<Object> objects = selectList(statementId, args);
                        return objects;
                    }
                    if(genericReturnType.equals(Void.TYPE)){
                        execute(statementId,args);
                        return true;
//                        execute(statementId,args);
//                        return true;
                    }
                    return selectOne(statementId, args);
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
        });

        return (T) proxyInstance;
    }


}